//
//  DownloadChapterVC.m
//  JW_BasePorject
//
//  Created by JW on 2018/7/2.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "DownloadChapterVC.h"

#import "PMDownloadModel+ZYVideo.h"
#import "CacheGroupModel.h"
#import "BJYFolderCell.h"

#import "DownloadSectionVC.h"

@interface DownloadChapterVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *chapterTableView;

@property (nonatomic, strong)NSMutableArray *chapterList;

@end

@implementation DownloadChapterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.chapterTableView];
    
    
    [self initChapterData];
}

- (void)initChapterData{
    
    NSMutableArray *finishedList = [NSMutableArray array];
    if([[PMDownloadManager downloadManager].finishedList count]) {
        finishedList = [NSMutableArray arrayWithArray:[PMDownloadManager downloadManager].finishedList];
    }
    
    NSMutableSet *set = [NSMutableSet set];
    [finishedList enumerateObjectsUsingBlock:^(PMDownloadModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([self.courseName isEqualToString:obj.course_name]) {
            NSString *str = [NSString stringWithFormat:@"%@-%@",obj.course_name,obj.chapter_name];
            [set addObject:str];
        }
    }];
    
    __block NSMutableArray *groupArr = [NSMutableArray array];
    [set enumerateObjectsUsingBlock:^(NSString * _Nonnull str, BOOL * _Nonnull stop) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"showFileName CONTAINS [cd] %@", str];
        NSArray *tempArr = [NSArray arrayWithArray:[finishedList filteredArrayUsingPredicate:predicate]];
        NSDictionary *downloadedDic = @{ @"showName": [str componentsSeparatedByString:@"-"][1],
                                         @"cacheState": @(ZYDownloaded),
                                         @"videoList" : tempArr
                                         };
        [groupArr addObject:[CacheGroupModel mj_objectWithKeyValues:downloadedDic]];
        MJExtensionLog(@"已下载:%@", [CacheGroupModel mj_objectWithKeyValues:downloadedDic]);

    }];
    
    //已缓存
    [self.chapterList addObjectsFromArray:groupArr];
    
    [self.chapterTableView reloadData];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chapterList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BJYFolderCell *cell = [BJYFolderCell cellWithTableView:tableView];
    cell.groupModel = self.chapterList[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CacheGroupModel *group = self.chapterList[indexPath.row];
    DownloadSectionVC *sectionVC = [[DownloadSectionVC alloc] init];
    sectionVC.courseName = self.courseName;
    sectionVC.chapterName = group.showName;
    [self.navigationController pushViewController:sectionVC animated:YES];
    
}



- (UITableView *)chapterTableView{
    if (_chapterTableView == nil) {
        _chapterTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBarHeight-36) style:UITableViewStylePlain];
        _chapterTableView.delegate = self;
        _chapterTableView.dataSource = self;
        [_chapterTableView registerClass:[BJYFolderCell class] forCellReuseIdentifier:@"BJYFolderCell"];
        _chapterTableView.rowHeight = UITableViewAutomaticDimension;
        // 设置tableView的估算高度
        _chapterTableView.estimatedRowHeight = 60;
        _chapterTableView.tableFooterView = [UIView new];
        
    }
    return _chapterTableView;
}

- (NSMutableArray *)chapterList{
    if (_chapterList == nil) {
        _chapterList = [NSMutableArray array];
    }
    return _chapterList;
}
@end
